package com.googlecode.jyoutubeuploader.util;

import java.util.Date;

public class StatisticsCalculator {
	public float getCompletedPercentage(long bytesRead, long fileSize){
		float bytesLeftToRead = fileSize - bytesRead;
		float completedPercentage = 100f - ((bytesLeftToRead / (float)fileSize) * 100f);
		return completedPercentage > 100f ? 100f : completedPercentage;
	}
	
	public float getEstimatedSpeed(long elapsedSeconds, long bytesRead){
		if (elapsedSeconds > 0){
			return ((float)bytesRead / (float)elapsedSeconds) / 1000f;
		}
		return 0;
	}
	
	public long getElapsedSeconds(Date startTime){
		Date now = new Date();
		return (now.getTime() - startTime.getTime()) / 1000;
	}

	public int getSecondSinceLastReport(Date lastReportTime) {
		Date now = new Date();
		return (int) ((now.getTime() - lastReportTime.getTime()) / 1000);
	}

	public int getPercentageOverSpeed(float estimatedSpeed,
			int throttleSpeedKbps) {
		int percentageOverSpeed = (int) (((estimatedSpeed / (float)throttleSpeedKbps) - 1) * 100);
		return percentageOverSpeed < 0 ? 0 : percentageOverSpeed;
	}
}
